package com.wez.controller.user;

import com.github.pagehelper.PageInfo;
import com.wez.entity.Result;
import com.wez.pojo.SysUser;
import com.wez.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

/**
 * @Date
 * @Author
 * @Description
 * @Version 1.0.0
 */
@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private ISysUserService sysUserService;

    /**
     * 去主页面
     * @return
     */
    @RequestMapping("/main")
    public String main(HttpSession httpSession,Model model){
        SysUser user = (SysUser)httpSession.getAttribute("USER_SESSION");
        model.addAttribute("name",user.getName());
        return "main";
    }

    /**
     * 去首页
     * @return
     */
    @RequestMapping("/index")
    public String index(){
        return "index";
    }
    /**
     * 去登录页面
     * @return
     */
    @RequestMapping("/to_login")
    public String toLogin(){
        return "login";
    }

    /**
     * 登录验证
     * @return
     */
    @RequestMapping("/login")
    public String login(SysUser sysUser, Model model, HttpSession httpSession){

        //安全验证
        if (sysUser==null || sysUser.getUsername()==null
                ||sysUser.getUsername().trim().equals("")
                ||sysUser.getPassword()==null
                ||sysUser.getPassword().trim().equals("")
        ){
            model.addAttribute("msg",
                    "请输入用户名或密码！");
            return "login";
        }
        SysUser user = sysUserService.getUserByUsernameAndPwd(sysUser);
        if (user!=null){
            httpSession.setAttribute("USER_SESSION",user);
            model.addAttribute("name",user.getName());
            return "main";
        }else {
            model.addAttribute("msg",
                    "用户名或密码错误！");
            return "login";
        }

    }
    /**
     * 用户列表页面
     * @return
     */
    @RequestMapping("/userList")
    public String userList(String username,Model model,
       @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
        @RequestParam(value = "pageSiz",defaultValue = "5")  Integer pageSiz
    ){

        List<SysUser> allUser = sysUserService.getAllUser(username, pageNum, pageSiz);
        PageInfo<Object> pageInfo = new PageInfo<>(allUser);
       // System.out.println(pageInfo);
        model.addAttribute("userList",allUser);
        model.addAttribute("pageInfo",pageInfo);

        model.addAttribute("username",username);
        return "user_list";
    }
    /**
     * 用户新增
     * @return
     */
    @RequestMapping("/addUser")
    @ResponseBody
    public Result addUser(SysUser sysUser){
        Result<Object> result = new Result<>();
        if (sysUser!=null
                && sysUser.getUsername()!=null
                && !sysUser.getUsername().trim().equals("")
                && sysUser.getPassword()!=null
                && !sysUser.getPassword().trim().equals("")
        ){
            System.out.println(sysUser);
            //是否重名
            SysUser user = sysUserService.getUserByUsername(sysUser.getUsername());
            if (user!=null){
                result.setMsg("用户已经存在！");
                result.setSuccess(false);
                return result;
            }
            int i = sysUserService.addUser(sysUser);
            if (i>0){
                System.out.println("新增成功！");
                result.setMsg("新增成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("新增失败！");
                result.setMsg("新增失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入用户名和密码！");
            result.setMsg("请输入用户名和密码！");
            result.setSuccess(false);
            return result;
        }

    }
    /**
     * 用户删除
     * @return
     */
    @RequestMapping("/delUser")
    @ResponseBody
    public Result delUser(Integer id){
        Result<Object> result = new Result<>();
        if (id!=null){
            int i = sysUserService.deleteUserById(id);
            if (i>0){
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择id！");
            result.setMsg("请选择id！");
            result.setSuccess(false);
            return result;
        }

    }
    /**
     * 批量删除
     * @return
     */
    @RequestMapping("/delAll")
    @ResponseBody
    public Result delAll(String ids){
        Result<Object> result = new Result<>();
        System.out.println("收到删除数据");
        System.out.println(ids);

        if (ids!=null && !ids.trim().equals("")){

            String[] strArray = ids.split(",");
            List<String> idsList = Arrays.asList(strArray);


            int i = sysUserService.deleteAll(idsList);
            if (i>0){
                System.out.println("删除成功！");
                result.setMsg("删除成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("删除失败！");
                result.setMsg("删除失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择数据！");
            result.setMsg("请选择数据！");
            result.setSuccess(false);
            return result;
        }

    }

    /**
     * 根据id查询用户
     * @return
     */
    @RequestMapping("/getUserById")
    @ResponseBody
    public Result getUserById(Integer id){
        Result<Object> result = new Result<>();
        if (id!=null){
            SysUser user = sysUserService.getUserById(id);

            if (user!=null){
                result.setData(user);
                System.out.println("成功！");
                result.setMsg("成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("没有此用户！");
                result.setMsg("没有此用户！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请选择id！");
            result.setMsg("请选择id！");
            result.setSuccess(false);
            return result;
        }
    }

    /**
     * 用户编辑
     * @return
     */
    @RequestMapping("/editUser")
    @ResponseBody
    public Result editUser(SysUser sysUser){
        Result<Object> result = new Result<>();
        if (sysUser!=null
                && sysUser.getUsername()!=null
                && !sysUser.getUsername().trim().equals("")
                && sysUser.getId()!=null
        ){

            int i = sysUserService.editUser(sysUser);
            if (i>0){
                System.out.println("修改成功！");
                result.setMsg("修改成功！");
                result.setSuccess(true);
                return result;
            }else {
                System.out.println("修改失败！");
                result.setMsg("修改失败！");
                result.setSuccess(false);
                return result;
            }

        }else{
            System.out.println("请输入用户名或id！");
            result.setMsg("请输入用户名或id！");
            result.setSuccess(false);
            return result;
        }

    }

    /**
     * 修改密码
     * @param oldPassword 旧密码
     * @param newPassword 新密码
     * @return 返回包含操作结果信息的Result对象
     */
    @RequestMapping("/updatePassword")
    @ResponseBody
    public Result updatePassword(String oldPassword, String newPassword) {
        Result<Object> result = new Result<>();
        if (oldPassword!= null && newPassword!= null &&!newPassword.trim().equals("")) {
            boolean success = sysUserService.updatePassword(oldPassword, newPassword);
            if (success) {
                result.setMsg("密码修改成功！");
                result.setSuccess(true);
            } else {
                result.setMsg("密码修改失败！");
                result.setSuccess(false);
            }
        } else {
            result.setMsg("请输入正确的旧密码！");
            result.setSuccess(false);
        }
        return result;
    }

        /**
         * 根据用户ID获取用户详情信息
         *
         * @param id 用户ID
         * @return 返回包含用户详情信息的Result对象
         */
        @RequestMapping("/getUserDetailById")
        @ResponseBody
        public Result getUserDetailById(Integer id) {
            Result<SysUser> result = new Result<>();
            if (id == null) {
                result.setMsg("用户ID不能为空");
                result.setSuccess(false);
                return result;
            }
            SysUser user = sysUserService.getUserDetailById(id);
            if (user!= null) {
                result.setData(user);
                result.setMsg("获取用户详情成功");
                result.setSuccess(true);
            } else {
                result.setMsg("未找到对应用户的详情信息");
                result.setSuccess(false);
            }
            return result;
        }
    }

